package com.ptszyxx.popose.module.base.splash;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YInitSdkUtil;
import com.ptszyxx.popose.common.utils.YMonitorUtil;
import com.ptszyxx.popose.module.chat.push.OfflineMessageBean;
import com.ptszyxx.popose.module.chat.push.OfflineMessageDispatcher;
import com.tencent.qcloud.tuicore.TUIConstants;
import com.ysg.http.HttpUtils;
import com.ysg.http.data.AppViewModelFactory;
import com.ptszyxx.popose.databinding.ActivitySplashBinding;
import com.ptszyxx.popose.module.base.splash.vm.SplashVM;
import com.ysg.base.BaseActivity;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.dialog.PrivacyDialog;

public class SplashActivity extends BaseActivity<ActivitySplashBinding, SplashVM> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_splash;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public SplashVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(SplashVM.class);
    }

    @Override
    public void initData() {
        //下载配置文件
        HttpUtils.getInstance().onDownloadAppConfig();
        //隐私政策
        boolean privacyFlag = YSPUtils.getInstance().getPrivacyFlag();
        if (!privacyFlag) {
            showDialogPrivacy();
            return;
        }
        //跳转
        jump();
    }

    private void showDialogPrivacy() {
        YDialogUtil.getInstance().showPrivacy(this)
                .setOnClickListener(new PrivacyDialog.OnClickListener() {
                    @Override
                    public void onAgreeClick(boolean isAgree) {
                        if (isAgree) {
                            YSPUtils.getInstance().savePrivacyFlag();
                            YInitSdkUtil.init(true);
                            jump();
                        } else {
                            finish();
                        }
                    }

                    @Override
                    public void onAgreementClick() {
                        YActivityUtil.getInstance().jumpAgreement(viewModel);
                    }

                    @Override
                    public void onPrivacyClick() {
                        YActivityUtil.getInstance().jumpPrivacy(viewModel);
                    }
                });
    }

    public void jump(){
        //ip白名单
        viewModel.requestIsWhiteIp();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e("是否登录", "是否登录:"+YSPUtils.getInstance().getLogin());
                if (YSPUtils.getInstance().getLogin()){
                    Intent dataIntent = getIntent();
                    String txUserid = null, userName = null, userPic = null;
                    if (dataIntent != null) {
                        //TODO 点击通知栏跳转
                        String ext = dataIntent.getStringExtra(TUIConstants.TUIOfflinePush.NOTIFICATION_EXT_KEY);
                        if (YStringUtil.isNotEmpty(ext)){
                            OfflineMessageBean bean = OfflineMessageDispatcher.getOfflineMessageBeanFromContainer(ext);
                            if (bean != null){
                                txUserid = bean.sender;
                                userName = bean.nickname;
                                userPic = bean.faceUrl;
                            }
                        }
                    }
                    YActivityUtil.getInstance().jumpMainActivity(viewModel, txUserid, userName, userPic);
                }else{
                    YActivityUtil.getInstance().jumpLoginQuickActivity(SplashActivity.this);
                }
                YActivityUtil.getInstance().close(SplashActivity.this);
            }
        }, 1000);
    }
}